//
//  TPArticleRootViewController.m
//  Yimei
//
//  Created by xiaowu xiaowu on 2017/10/20.
//  Copyright © 2017年 xiaowu xiaowu. All rights reserved.
//

#import "TPArticleRootViewController.h"
#import "TPArticleCell.h"
#import "TPArticleDetailViewController.h"
#import "TPArticleListModel.h"
@interface TPArticleRootViewController ()

@end

@implementation TPArticleRootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"内容";
    [self setup];
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.tableView reloadData];
}
-(void)setup{
    
    self.tableViewStyle = UITableViewStyleGrouped;
    self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 50.0, 0);
//    self.tableView.backgroundColor = TP_COLOR_245;
    
//    TPArticleCommentModel *model = [TPArticleCommentModel getTPArticleCommentModelName:@"揭秘|实力派如何提高自体脂肪存活率" content:@"做自体脂肪移植，小伙伴们最关心的问题就是脂肪的“存活率”了。据做过脂肪填充的美眉们说，脂肪存活率通常低至50%以下？！" url:@"http://hera.s.igengmei.com/slide/2016/05/30/78358e5146"];
//    TPArticleCommentModel *model1 = [TPArticleCommentModel getTPArticleCommentModelName:@"气质瞬间up的7个肉毒素妙用" content:@"斜方肌是链接肩膀与手臂的肌肉，从颈部后侧沿着颈椎的方向向下，并往两侧肩膀水平延伸" url:@"http://hera.s.igengmei.com/2016/06/20/ce6e5f8431"];
//    TPArticleCommentModel *model2 = [TPArticleCommentModel getTPArticleCommentModelName:@"【医美小百科】涨姿势|果酸换肤不是你想换就能换~" content:@"想要皮肤细嫩、光滑、粗糙，很多美眉们会选择果酸换肤（俗称刷酸）来保养。网上有超多刷酸经验贴、果酸产品的比较" url:@"http://hera.s.igengmei.com/2016/06/13/56075d4bce-w"];
//    TPArticleCommentModel *model3 = [TPArticleCommentModel getTPArticleCommentModelName:@"特别观察员|三分钟揭露披着羊皮的奥美定" content:@"打针就能立刻变美使得很多人对注射美容趋之若鹜，然而在一些非正规整形市场上，无职业资格执照的江湖游医仍打着玻尿酸的名义注射奥美定，被判死刑的奥美定仍然祸乱人间" url:@"http://hera.s.igengmei.com/slide/2016/06/17/4a8cc91c5c"];
  
    @weakify(self);
    [self getRequest];
    [self.tableView xw_addPullToRefreshWithHandler:^(UIScrollView *scrollView) {
        @strongify(self);
        self.page = 1;
        [self getRequest];
        
    }];
    
    
    [self.tableView xw_addInfinityScrollWithHandler:^(UIScrollView *scrollView) {
        @strongify(self);
        self.page ++;
        [self getRequest];
    }];
}

-(void)getRequest{
    
    [[TPRequestClient sharedClient]getArticlePinglunList_current:self.page size:kMaxRequestSize success:^(NSURLSessionDataTask *task, id responseObject, Responese *respone) {
        if(self.page == 1)
            [self.dataSource removeAllObjects];
        NSArray *array = [NSArray yy_modelArrayWithClass:[TPArticleListModel class] json:respone.data[@"data"]];
        [self.dataSource addObjectsFromArray:array];
        [self refreshSccuess];
        
    } failure:^(NSURLSessionDataTask *task, NSError *error, Responese *respone) {
        [self refreshFail];
    } nonetWork:^(NSURLSessionDataTask *task, NSError *error) {
        [self refreshFail];
    }];
    
}


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataSource.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
   
    return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
            TPArticleCell *cell =[tableView dequeueReusableCellWithIdentifier:@"TPHomeCell"];
            if(!cell){
                cell = [[TPArticleCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"TPHomeCell"];
            }
            cell.indexPath = indexPath;
            cell.model = self.dataSource[indexPath.section];
            return cell;
   
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    TPArticleDetailViewController *articleVC = [TPArticleDetailViewController new];
    articleVC.model = self.dataSource[indexPath.section];
    [self.navigationController pushViewController:articleVC animated:YES];
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
  
    return 0.01;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 5.0f;
}



@end
